package com.hbnu.controller;

import com.hbnu.mapper.OrderMapper;
import com.hbnu.pojo.Collection;
import com.hbnu.pojo.Good;
import com.hbnu.pojo.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

@Configuration
@Component
public class Clock implements SchedulingConfigurer {
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addFixedDelayTask(this::index2, 1000);
    }
    public void index2() {
        List<String> oidList=orderMapper.getOidList();
        oidList.stream().forEach(oid->{
            Order order=orderMapper.getOrderDetail(oid);
            LocalDateTime curTime=LocalDateTime.now();
            long second= ChronoUnit.SECONDS.between( order.getTime(),curTime);
            if(second>3600&&order.getState()==1) {
                order.setState(2);
                orderMapper.updateOrderState(oid, 2);
                List<Collection> goodsList = orderMapper.getGoodsListByOid(oid);
                goodsList.stream().forEach(goods -> {
                    orderMapper.updateGoodNum(goods.getGid(), -goods.getCount());
                });
            }
        });
    }


}
